<template>
  <div class="tabsBox">
    <div :class="scrollTops ? 'positionTab' : ''">
      <div class="tabs">
        <div class="menuBox">
          <div
            v-for="item in menuList"
            :key="item.id"
            @click="handleClick(item)"
            @mouseover="hoverItem(item)"
            class="tabList"
            :class="{ active: item.field == activeName }"
          >
            <span>{{ item.name + '(' + item.totalCount + ')' }}</span>
            <el-icon :class="['el-icon-arrow-up', 'icon']">
              <ArrowDown />
            </el-icon>
          </div>
        </div>
        <div @mouseleave="mouseleaveBox" class="hoverBox" v-if="chageItem.length">
          <el-button
            @click="scropPositon(items)"
            v-for="items in chageItem"
            :key="items.id"
            :class="items.totalCount == 0 ? 'primry' : 'isprimry'"
            :disabled="items.totalCount == 0"
            >{{ items.name + '(' + items.totalCount + ')' }}</el-button
          >
        </div>
      </div>
    </div>
    <div class="content" style="z-index: 0">
      <ContentScorll
        :value="value"
        :cid="cid"
        :numberCount="numberCount"
        :listMap="listMap"
        ref="contenScroll"
      />
    </div>
  </div>
</template>
<script setup lang="ts">
import { ArrowDown } from '@element-plus/icons-vue'
import ContentScorll from './ContentScorll.vue'
const contenScroll = ref(null)
const chageItem = ref([])
const activeName = ref('')
const value = ref('')
const cid = ref('')
const numberCount = ref('')
const scrollTops = ref(false)
const props = defineProps({
  menuList: Array,
  companyInfo: Object,
  idx: String || Number,
  listMap: Object
})
watch(
  () => props,
  (val) => {
    cid.value = val.companyInfo.cid
    value.value = val.menuList[props.idx]
    activeName.value = val.menuList[props.idx].field
    numberCount.value = val.menuList[props.idx].totalCount
  },
  { deep: true }
)

// 处理滚动位置锚点
const scropPositon = async (item) => {
  const values = value.value.detailMenuVO.map((init) => init.field == item.field)
  if (values.indexOf(true) > -1) {
    contenScroll.value.scrollToItem(item.id)
  } else {
    const id = Number(String(item.id).charAt(0))
    const data = props.menuList[id - 1]
    value.value = data
    activeName.value = data.field
    numberCount.value = data.totalCount
    nextTick(() => {
      contenScroll.value?.scrollToItem(item.id)
    })
  }
}

const handleClick = (item) => {
  value.value = item
  activeName.value = item.field
  numberCount.value = item.totalCount
}

const hoverItem = (item) => {
  chageItem.value = item.detailMenuVO
}

const mouseleaveBox = (item) => {
  chageItem.value = []
}

const setScrollTops = (val) => {
  scrollTops.value = val
}
defineExpose({ mouseleaveBox, setScrollTops })
</script>

<style lang="scss" scoped>
.tabsBox {
  width: 100%;
  background: #fff;
  .positionTab {
    position: sticky;
    top: 52px;
    z-index: 2;
    background: #fff;
    z-index: 10;
  }
  .tabs {
    width: 100%;
    color: #d6d6d8;
    font-size: 14px;
    position: relative;
    .hoverBox {
      position: absolute;
      top: 40px;
      width: calc(100% - 40px);
      left: 0;
      background-color: #fff;
      box-shadow: 0px 5px 5px -5px rgba(0, 0, 0, 0.1);
      padding: 20px;
      display: flex;
      flex-wrap: wrap;
      align-items: flex-start;
      z-index: 10;
      .el-button {
        margin: 0;
        margin-right: 10px;
        margin-bottom: 10px;
      }
      .isprimry {
        font-size: 12px;
        padding: 10px;
        border-radius: 4px;
        color: #333;
        border: 1px solid #d6d6d8;
      }
      .isprimry:hover {
        color: #4f7adf;
        background-color: #fff;
        border-color: #4f7adf;
      }
    }
    .menuBox {
      width: 100%;
      display: flex;
      padding-bottom: 4px;
      border-bottom: 1px solid #d6d6d8;
      .tabList {
        display: flex;
        flex: 1;
        justify-content: center;
        align-items: center;
        padding: 10px;
        cursor: pointer;
        position: relative;
        background-color: #fff;
        color: #333;
        &.active {
          color: #4f7adf !important;
          &::after {
            content: '';
            position: absolute;
            left: 42%;
            transform: translateX(-50%);
            bottom: 0;
            width: 24px;
            height: 2px;
            background: #4f7adf;
          }
        }
      }
    }

    .tabList:hover {
      color: #b8b2ff;
    }
    .content {
      width: 100%;
      overflow: auto;
    }
  }
}
</style>
